﻿var storeClient = createClientStore();
var storeLicensePlate = createVehicleStore();
var storeAlarmType = createVehicleStore();
function createAlarmQueryPanel() {
    return new Ext.Panel({
        region: 'center',
        id: 'app-alarm-panel',
        title: '报警查询',
        anchor: '100% 100%',
        closable: true,
        autoScroll: true,
        containerScroll: true,
        //margins: '0 0 0 5',
        border: false,
        iconCls: 'nav',
        items: [createConditions(), {
            layout: 'fit',
            items: [createResult()]
        }]
    });
}

//function createAlarmQueryPanel() {
//    return new Ext.Panel({
//        region: 'center',
//        id: 'app-alarm-panel',
//        title: '报警查询',
//        anchor: '100% 100%',
//        closable: true,
//        margins: '0 0 0 5',
//        layout: {
//            type: 'accordion', 
//            animate: true
//        },
//        items: [{
//            border: false,
//            iconCls: 'nav',
//            items: [createConditions(), createResult()]
//    }]
//    });
//}
function createConditions() {
    storeClient.load({ params: { clientNo: userClientNo, currentService: currentService, op: 'strClientInfoList'} });
    storeLicensePlate.load({ params: { clientNo: userClientNo, currentService: currentService, op: 'strVehicleInfoList'} });
    storeAlarmType.load({ params: { op: 'strAlarmAllType'} });
    return new Ext.Panel({
        labelAlign: 'top',
        frame: true,
        //title: '查询条件',
        bodyStyle: 'padding:0px 0px 0',
        //width: 720,
        items: [{
            layout: 'column',
            items: [{
                columnWidth: .5,
                layout: 'form',
                items: [inicmbAlarm_Group(),
                        inicmbAlarm_Type()]
            },
                {
                    columnWidth: .5,
                    layout: 'form',
                    items: [
                        createcmbLicensePlateAlarm(),
                        inicmbFLAG()
                    ]
                },
                {
                    columnWidth: .5,
                    layout: 'form',
                    items: [createStartDate()]
                },
                {
                    columnWidth: .5,
                    layout: 'form',
                    items: [createEndDate()]
                }
                ]
        }],

        buttonAlign: 'center',
        buttons: [{
            text: "查询",
            name: "button",
            handler: function () {

                //Ext.Msg.alert("值", "实际值：" + cmbLicensePlate.getValue()() + ";显示值:" + cmbLicensePlate.getRawValue());
                if (Ext.getCmp('cmbAlarm_GroupAlarm').getValue() == "" || Ext.get('cmbAlarm_GroupAlarm').dom.value == "请选择车辆分组") {
                    Ext.MessageBox.alert("提示", "请选择车辆分组");
                    return;
                }
                if (Ext.getCmp('cmbLicensePlateAlarm').getValue() == "" || Ext.get('cmbLicensePlateAlarm').getValue() == "请选择车辆名称") {
                    Ext.MessageBox.alert("提示", "请选择名称");
                    return;
                }
                if (Ext.getCmp('cmbAlarm_TypeAlarm').getValue() == "" || Ext.get('cmbAlarm_TypeAlarm').getValue() == "请选择报警类型") {
                    Ext.MessageBox.alert("提示", "请选择报警类型");
                    return;
                }
                if (currentService == 5) {
                    if (Ext.get('cmbFLAG').getValue() == "请选择处理状态") {
                        Ext.MessageBox.alert("提示", "请选择处理状态");
                        return;
                    }
                }
                if (Ext.getCmp('StartDate').getValue() == "" || Ext.get('StartDate').getValue() == "请选择开始时间") {
                    Ext.MessageBox.alert("提示", "请选择开始时间");
                    return;
                }
                if (Ext.getCmp('EndDate').getValue() == "" || Ext.get('EndDate').getValue() == "请选择结束时间") {
                    Ext.MessageBox.alert("提示", "请选择结束时间");
                    return;
                }
                storeGrid.removeAll();
                if (currentService == 5) {
                    var stimeAlarm = dataFormat(Ext.getCmp('StartDate').getValue(), "yyyy");
                    var etimeAlarm = dataFormat(Ext.getCmp('EndDate').getValue(), "yyyy");
                    storeGrid.load({ params: { clientNo: userClientNo, currentService: currentService, op: 'GetAlarmAllSelect', StartTime: Ext.get('StartDate').getValue(), EndTime: Ext.get('EndDate').getValue(), AlarmType: Ext.getCmp('cmbAlarm_TypeAlarm').getValue(), Flag: "", LicensePlate: Ext.get('cmbLicensePlateAlarm').getValue()} });

                } else {
                    //                    storeGrid.load({ params: { clientNo: userClientNo, currentService: currentService, op: 'strAlarms', StartTime: Ext.get('StartDate').getValue(), EndTime: Ext.get('EndDate').getValue(), AlarmType: Ext.getCmp('cmbAlarm_TypeAlarm').getValue(), Flag: Ext.getCmp('cmbFLAG').getValue(), LicensePlate: Ext.get('cmbLicensePlateAlarm').getValue()} });
                    storeGrid.load({ params: { clientNo: userClientNo, op: 'strAlarms', limit: '10', start: '1', sort: 'Alarm_Time', dir: '', StartTime: Ext.get('StartDate').getValue(), EndTime: Ext.get('EndDate').getValue(), AlarmType: Ext.getCmp('cmbAlarm_TypeAlarm').getValue(), Flag: Ext.getCmp('cmbFLAG').getValue(), LicensePlate: Ext.get('cmbLicensePlateAlarm').getValue()} });

                }

            }
        }]
    });
}

function createResult() {
    // 列
    var cm;
    if (currentService == "5") {
        cm = new Ext.grid.ColumnModel([
        	{ header: "名称", dataIndex: "LICENSE_PLATE", width: 80 },
    { header: "报警类型", dataIndex: "ALARM_TYPE", width: 60 },
        //	{ header: "通讯卡号", dataIndex: "SIMCARD_NO", width: 90 },
	{header: "报警时间", dataIndex: "ALARM_TIME", renderer: renderDate, width: 120 },
    { header: "报警位置", dataIndex: "ALARM_LOCATION", width: 60 },
	{ header: "经度", dataIndex: "LONGITUDE", width: 80 },
	{ header: "纬度", dataIndex: "LATITUDE", width: 75 }
    ]);

        // 数据源
        storeGrid = new Ext.data.Store({
            url: "../Json/JsonGetAlarmQueryInfoList.ashx",
            reader: new Ext.data.JsonReader(
            {
                root: 'data'
            },
            Ext.data.Record.create([
	{ name: 'VID' },
	{ name: 'LICENSE_PLATE' },
	{ name: 'SIMCARD_NO' },
    { name: 'ALARM_LOCATION' },
    { name: 'ALARM_TIME' },
	{ name: 'LONGITUDE' },
	{ name: 'LATITUDE' },
	{ name: 'ALARM_TYPE' }
	])
        ),
            remoteSort: true                         // 服务端排序
        });
        storeGrid.setDefaultSort('Alarm_Time', 'desc');    // 默认按 Date 列降序排列
    } else {
        cm = new Ext.grid.ColumnModel([
        { header: '名称', dataIndex: 'License_Plate', sortable: true, width: 60 },
        { header: '报警类型', dataIndex: 'Alarm_Type', sortable: true, renderer: GetAlarmTypeString, width: 64 },
        { header: '经度', dataIndex: 'LONGITUDE', sortable: true, width: 58 },
        { header: '纬度', dataIndex: 'LATITUDE', sortable: true, width: 52 },
        //{ header: '报警区域', dataIndex: 'Alarm_Location', sortable: true, width: 0 },
        {header: '报警时间', dataIndex: 'Alarm_Time', sortable: true, width: 90 },   // 在 renderDate 函数中格式化
        {header: '处理状态', dataIndex: 'FLAG', sortable: true, renderer: GetFlag, width: 52 },
        //{ header: '速度', dataIndex: 'Speed', sortable: true, width: 0 },
        {header: '确认人员', dataIndex: 'OPERATOR', sortable: true, width: 80 },
        { header: '确认时间', dataIndex: 'DISPOSE_TIME', sortable: true, width: 90 }
    ]);

        // 数据源
        storeGrid = new Ext.data.Store({
            url: "../Json/JsonGetAlarmQueryInfoList.ashx",
            reader: new Ext.data.JsonReader(
            {
                root: 'data'
            },
            Ext.data.Record.create([
	{ name: 'License_Plate' },
    { name: 'Alarm_Type' },
    { name: 'LONGITUDE' },
    { name: 'LATITUDE' },
            //{ name: 'Alarm_Location' },
    {name: 'Alarm_Time' },
    { name: 'FLAG' },
            //{ name: 'Speed' },
    {name: 'OPERATOR' },
    { name: 'DISPOSE_TIME' }
	])
        ),
            remoteSort: true                         // 服务端排序
        });
        storeGrid.setDefaultSort('Alarm_Time', 'desc');    // 默认按 Date 列降序排列
    }


    return new Ext.grid.GridPanel({
        //frame: true,
        anchor: '100%',
        layout: 'fit',
        enableHdMenu: false,
        autoScroll: true,
        containerScroll: true,
        //width: 720,
        //height: 400,
        autoHeight: true,
        //title: '报警列表',
        loadMask: { msg: '正在加载数据，请稍侯……' },
        //el: 'grid',
        store: storeGrid,
        cm: cm,
        //bbar: bbar,
        bodyStyle: 'height:100%',
        viewConfig: {
            forceFit: true
        }
    });
}


function createClientStore() {
    var ClientStoreRecord = Ext.data.Record.create([
	{ name: 'CLIENT_NAME' },
	{ name: 'CLIENT_NO' }
	]);
    // 数据源
    return new Ext.data.Store({
        url: "./Json/JsonGetAlarmQueryInfoList.ashx",
        reader: new Ext.data.JsonReader({ root: 'data' }, ClientStoreRecord)
        // ,
        //    autoLoad: true,
        //    remoteSort: true// 服务端排序
    });
}


function createVehicleStore() {
    return new Ext.data.Store({
        url: "./Json/JsonGetAlarmQueryInfoList.ashx",
        reader: new Ext.data.JsonReader(
            {
                root: 'data'
            },
            Ext.data.Record.create([
	{ name: 'LICENSE_PLATE' },
    { name: 'VID' }
	])
        ),
        autoLoad: true,
        remoteSort: true// 服务端排序
    });
}
function inicmbLicensePlate(value) {

    //storeLicensePlate.load({ params: { clientNo: value, op: 'strVehicleInfoList'} });
    storeLicensePlate.removeAll();
    if (currentService == 5) {
        storeLicensePlate.load({
            params: { clientNo: userClientNo, currentService: value, op: 'strVehicleInfoList' }
        });
    } else {
        storeLicensePlate.load({
            params: { clientNo: value, currentService: currentService, op: 'strVehicleInfoList' }
            //            ,
            //                    callback: function(records, totlalCount, success) {
            //                        alert("totlalCount:" + records.length);
            //                        alert("success:" + success);
            //                    }
        });
    }

}

function createcmbLicensePlateAlarm() {
    var box = new Ext.form.ComboBox({
        fieldLabel: '名称',
        id: 'cmbLicensePlateAlarm',
        width: 180,
        allowBlank: false,
        triggerAction: "all", //是否开启自动查询功能
        store: storeLicensePlate, //定义数据源
        displayField: "LICENSE_PLATE", //关联某一个逻辑列名作为显示值
        valueField: "VID", //关联某一个逻辑列名作为实际值
        mode: "local", //如果数据来自本地用local 如果来自远程用remote默认为remote
        emptyText: "请选择车辆名称"//没有选择时候的默认值
    })
    Ext.getCmp("cmbLicensePlateAlarm").setValue("全部");
    return box;
}

function inicmbAlarm_Group() {
    if (currentService == 5) {
        var cities = [[5, "全部"], [2, "环卫工"], [1, "机扫车"], [3, "垃圾车"]];
        var proxy = new Ext.data.MemoryProxy(cities);
        var City = Ext.data.Record.create([
     { name: "cid", type: "int", mapping: 0 },
     { name: "cname", type: "string", mapping: 1 }
 ]);
        var reader = new Ext.data.ArrayReader({}, City);
        var store = new Ext.data.Store({
            proxy: proxy,
            reader: reader//,
            //autoLoad:true
        });
        store.load();
        //定义一个下拉框
        var box = new Ext.form.ComboBox({
            fieldLabel: '分组',
            id: 'cmbAlarm_GroupAlarm',
            width: 180,
            triggerAction: "all", //是否开启自动查询功能
            store: store, //定义数据源
            displayField: "cname", //关联某一个逻辑列名作为显示值
            valueField: "cid", //关联某一个逻辑列名作为实际值
            mode: "local", //如果数据来自本地用local 如果来自远程用remote默认为remote
            emptyText: "请选择车辆分组", //没有选择时候的默认值
            listeners: {
                select: function (combo, record, index) {
                    try {
                        inicmbLicensePlate(this.value);
                    }
                    catch (ex) {
                        Ext.MessageBox.alert("错误", "数据加载失败。");
                    }
                }
            }
        });
        Ext.getCmp("cmbAlarm_GroupAlarm").setValue("全部");
        return box;
    } else {
        var box = new Ext.form.ComboBox({
            fieldLabel: '分组',
            id: 'cmbAlarm_GroupAlarm',
            hiddenName: 'CLIENT_NO',
            width: 180,
            allowBlank: false,
            triggerAction: "all", //是否开启自动查询功能
            store: storeClient, //定义数据源
            displayField: "CLIENT_NAME", //关联某一个逻辑列名作为显示值
            valueField: "CLIENT_NO", //关联某一个逻辑列名作为实际值
            mode: "local", //如果数据来自本地用local 如果来自远程用remote默认为remote
            emptyText: "请选择车辆分组", //没有选择时候的默认值

            listeners: {
                select: function (combo, record, index) {
                    try {
                        inicmbLicensePlate(this.value);
                    }
                    catch (ex) {
                        Ext.MessageBox.alert("错误", "数据加载失败。");
                    }
                }
            }
        });
        Ext.getCmp("cmbAlarm_GroupAlarm").setValue("全部");
        return box;
    }
}

function inicmbAlarm_Type() {
    var box = new Ext.form.ComboBox({
        fieldLabel: '报警类型',
        id: 'cmbAlarm_TypeAlarm',
        width: 180,
        allowBlank: false,
        triggerAction: "all", //是否开启自动查询功能
        store: storeAlarmType, //定义数据源
        displayField: "LICENSE_PLATE", //关联某一个逻辑列名作为显示值
        valueField: "VID", //关联某一个逻辑列名作为实际值
        mode: "local", //如果数据来自本地用local 如果来自远程用remote默认为remote
        emptyText: "请选择报警类型"//没有选择时候的默认值
    });
    Ext.getCmp("cmbAlarm_TypeAlarm").setValue("全部");
    return box;
    //    var cities = [[-1, "全部"], [4, "超速报警"], [2, "紧急报警"], [3, "电子围栏报警"], [41, "出河西区报警"], [103, "GPS天线报警"], [110, "停留报警"], [111, "进区域报警"], [112, "出区域报警"], [7, "油耗报警"]];

    //    //Ext.data.DataProxy是获取数据代理 一般使用他的子类 MemoryProxy ,HttpProxy , ScriptTagProxy
    //    /* MemoryProxy 获取来自内存的数据，可以是数组，json或者xml
    //    HttpProxy 使用http协议通过ajax从远程服务器获取数据的代理
    //    ScriptTagProxy 功能和HttpProxy一样，支持跨域获取数据
    //    */
    //    var proxy = new Ext.data.MemoryProxy(cities);
    //    //var proxy=new Ext.form.ComboBox(cities);
    //    var City = Ext.data.Record.create([
    //    //City结构 name和type表示每一列的名称和数据类型 mapping是列值与数组元素的映射关系
    //     {name: "cid", type: "int", mapping: 0 },
    //     { name: "cname", type: "string", mapping: 1 }
    // ]);
    //    //Ext.data.DataReader有三个子类 数组用ArrayReader  json用JsonReader  xml用XmlReader
    //    var reader = new Ext.data.ArrayReader({}, City);
    //    //var reader=new Ext.data.ArrayReader({},[
    //    //{name:"cid",type:"int",mapping:0},
    //    //{name:"cname",type:"string",mapping:1}
    //    // ]);

    //    // Ext。data成了数据来源  
    //    /*  DataProxy获取想要的数据，通过他恩那个得到来自不同地方的数据，如数组，远程服务器等
    //    DataReader定义数据项的逻辑结构
    //    store存储器 用于整合DataProxy和DataReader
    //    */
    //    var store = new Ext.data.Store({
    //        proxy: proxy,
    //        reader: reader//,
    //        //autoLoad:true
    //    });
    //    store.load();
    //    //定义一个下拉框
    //    return new Ext.form.ComboBox({
    //        fieldLabel: '报警类型',
    //        id: 'cmbAlarm_TypeAlarm',
    //        width: 180,
    //        triggerAction: "all", //是否开启自动查询功能
    //        store: store, //定义数据源
    //        displayField: "cname", //关联某一个逻辑列名作为显示值
    //        valueField: "cid", //关联某一个逻辑列名作为实际值
    //        mode: "local", //如果数据来自本地用local 如果来自远程用remote默认为remote
    //        emptyText: "请选择报警类型"//没有选择时候的默认值
    //    });
    //    //    cmbAlarm_Type.getStore().addListener(
    //    //        {
    //    //            load: function(store) {
    //    //            cmbAlarm_Type.setValue("-1");
    //    //            }
    //    //        });
}

function inicmbFLAG() {
    if (currentService == 5) {
        return new Ext.form.Label({ id: "cmbFLAG", width: 300, value: "" });
    }
    var cities = [[-1, "全部"], [0, "未确认"], [1, "已确认"]];

    //Ext.data.DataProxy是获取数据代理 一般使用他的子类 MemoryProxy ,HttpProxy , ScriptTagProxy
    /* MemoryProxy 获取来自内存的数据，可以是数组，json或者xml
    HttpProxy 使用http协议通过ajax从远程服务器获取数据的代理
    ScriptTagProxy 功能和HttpProxy一样，支持跨域获取数据
    */
    var proxy = new Ext.data.MemoryProxy(cities);
    //var proxy=new Ext.form.ComboBox(cities);
    var City = Ext.data.Record.create([
    //City结构 name和type表示每一列的名称和数据类型 mapping是列值与数组元素的映射关系
     {name: "cid", type: "int", mapping: 0 },
     { name: "cname", type: "string", mapping: 1 }
 ]);
    //Ext.data.DataReader有三个子类 数组用ArrayReader  json用JsonReader  xml用XmlReader
    var reader = new Ext.data.ArrayReader({}, City);
    //var reader=new Ext.data.ArrayReader({},[
    //{name:"cid",type:"int",mapping:0},
    //{name:"cname",type:"string",mapping:1}
    // ]);

    // Ext。data成了数据来源  
    /*  DataProxy获取想要的数据，通过他恩那个得到来自不同地方的数据，如数组，远程服务器等
    DataReader定义数据项的逻辑结构
    store存储器 用于整合DataProxy和DataReader
    */
    var store = new Ext.data.Store({
        proxy: proxy,
        reader: reader//,
        //autoLoad:true
    });

    store.load();
    //定义一个下拉框
    var box = new Ext.form.ComboBox({
        fieldLabel: '处理状态',
        id: 'cmbFLAG',
        width: 180,
        allowBlank: false,
        triggerAction: "all", //是否开启自动查询功能
        store: store, //定义数据源
        displayField: "cname", //关联某一个逻辑列名作为显示值
        valueField: "cid", //关联某一个逻辑列名作为实际值
        mode: "local", //如果数据来自本地用local 如果来自远程用remote默认为remote
        emptyText: "请选择处理状态"//没有选择时候的默认值

    });
    //    store.addListener(
    //        {
    //            load: function(store) {
    //                cmbFLAG.setValue("-1");
    //            }
    //        });
    Ext.getCmp("cmbFLAG").setValue("全部");
    return box;
}

function createStartDate() {
    return new Ext.ux.form.DateTimeField({
        fieldLabel: '开始时间',
        width: 180,
        format: 'Y-m-d',
        allowBlank: false,
        id: "StartDate"
    });
}


function createEndDate() {
    return new Ext.ux.form.DateTimeField({
        fieldLabel: '结束时间',
        width: 180,
        format: 'Y-m-d',
        allowBlank: false,
        id: "EndDate"
    });
}